package in.iqing.control.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.makeramen.roundedimageview.RoundedTransformationBuilder;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Transformation;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.control.helper.ImageResourceHelper;
import in.iqing.control.helper.LogHelper;
import in.iqing.control.util.MiscUtil;
import in.iqing.control.util.ViewUtil;
import in.iqing.model.bean.Recommend;

/**
 * Created by ZuoShu on 15/7/3.
 */
public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.RecommendViewHolder> {
    private static final String TAG = "RecommendAdapter";
    private Context context;
    private List<Recommend> recommends;
    private static final int COVER_RATIO = 3;
    private int recommendWidth;
    private int recommendHeight;
    private Transformation coverTransformation;
    private OnRecommendClickListener listener;

    public interface OnRecommendClickListener {
        public void onRecommendClick(Recommend recommend);
    }

    public RecommendAdapter(Context context) {
        this.context = context;
        calculateSize();
        coverTransformation = new RoundedTransformationBuilder()
                .cornerRadius(context.getResources().getDimension(R.dimen.round_image_radius))
                .borderWidth(context.getResources().getDimension(R.dimen.image_border_width))
                .borderColor(context.getResources().getColor(R.color.iqing_yellow))
                .oval(false)
                .build();
    }

    public void setListener(OnRecommendClickListener listener) {
        this.listener = listener;
    }

    public void setRecommends(List<Recommend> recommends) {
        this.recommends = recommends;
    }

    private void calculateSize() {
        recommendWidth = (int) (MiscUtil.getScreenWidth(context)
                - 2 * context.getResources().getDimension(R.dimen.recommend_horizontal_margin));
        recommendHeight = recommendWidth / COVER_RATIO;
        LogHelper.warn(TAG, "recommend item width:" + recommendWidth + " height:" + recommendHeight);
    }

    @Override
    public RecommendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend, parent, false);
        return new RecommendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecommendViewHolder holder, int position) {
        Recommend recommend = recommends.get(position);
        RequestCreator createor = TextUtils.isEmpty(recommend.getCover())
                ? Picasso.with(context).load(R.drawable.image_loading_1x1)
                : Picasso.with(context).load(ImageResourceHelper.generateSlideImageUrl(recommend.getCover()));
        createor.placeholder(R.drawable.image_loading_3x1)
                .error(R.drawable.image_error)
                .transform(coverTransformation)
                .resize(recommendWidth, recommendHeight)
                .centerCrop()
                .into(holder.coverImage, new FixSizeCallback(holder.coverImage));
        holder.recommend = recommend;
    }

    @Override
    public int getItemCount() {
        return recommends == null ? 0 : recommends.size();
    }

    class RecommendViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.cover_image)
        ImageView coverImage;

        Recommend recommend;

        public RecommendViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.cover_image)
        public void onCoverClick(View v) {
            if (listener != null) {
                listener.onRecommendClick(recommend);
            }
        }
    }

    private class FixSizeCallback implements Callback {
        ImageView imageView;

        public FixSizeCallback(ImageView imageView) {
            this.imageView = imageView;
        }

        @Override
        public void onSuccess() {
            if (imageView != null) {
                ViewUtil.fixSize(imageView, recommendWidth, recommendHeight);
            }
        }

        @Override
        public void onError() {

        }
    }

}
